package com.edu.filter;

import com.edu.commons.JWTUtils;
import com.edu.commons.ServiceCode;
import com.edu.commons.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: myApp
 * @Titile: AuthInterceptor
 * @Author: Administrator
 * @Description: token拦截器
 */

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取客户端传递的token [Authorization]
        String token = request.getHeader("Authorization");
        if(token==null || token.isBlank()){
            //客户端没有带token
            throw new ServiceException(ServiceCode.ERROR_TOKEN,"用户身份不能为空");
        }
        //校验这个token是否合法
        JWTUtils.validateToken(token);
        // todo 把合法token解析出来，封装好，存进一个作用域
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
